<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Balance;
use App\Models\Merchant;
use App\Models\User;
use Illuminate\Http\Request;

class MerchantController extends BaseController
{

    public $model = Merchant::class;

    public function index(Request $request) {
        $phone = $request->input('phone');
        $weixin = $request->input('weixin');
        $contact = $request->input('contact');
        $id = $request->input('id');
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');
        $audit_status = $request->input('audit_status', -1);
        $is_black = $request->input('is_black', -1);

        $order_by_key = $request->input('order_by_key', 'audit_status');
        $order_by_value = $request->input('order_by_value', 'asc');


        $list = Merchant::where('category', 1)->when($phone, function ($q)use($phone) {
            $q->where('phone', 'like', '%'. $phone . '%');
        })->black($is_black)->when($weixin, function ($q)use($weixin) {
            $q->where('weixin', 'like', '%'. $weixin . '%');
        })->when($contact, function ($q)use($contact) {
            $q->where('contact', 'like', '%'. $contact . '%');
        })->when($start_at, function ($q)use($start_at) {
            $q->where('start_at', '>=',  $start_at );
        })->when($end_at, function ($q)use($end_at) {
            $end_at = date('Y-m-d', strtotime($end_at) + 24 * 3600);
            $q->where('created_at', '<', $end_at);
        })->when($audit_status != -1, function ($q)use($audit_status) {
            $q->where('audit_status', '=',  $audit_status );
        })->when($id, function ($q)use($id) {
            $q->where('id', '=',  $id );
        })->orderBy($order_by_key, $order_by_value)->orderBy('id', 'desc')
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }


    public function child(Request $request) {
        $input = $request->input();
        $search = ['start_at' => '', 'end_at' => ''];
        $order_by_key = $request->input('order_by_key', 'child_count');
        $order_by_value = $request->input('order_by_value', 'desc');
        $id = $input['id'] ?? 0;
        $benefit_user = $input['benefit_user'] ?? User::BENEFIT_DAREN;
        if ($benefit_user == User::BENEFIT_DAREN) {
            $model = new Merchant();
        } else {
            $model = new User();
            $model = $model->when($benefit_user, function ($q) use($benefit_user) {
                $q->where('benefit_user', $benefit_user);
            });
        }

        $list = $model->apply($search, $input)->when($id, function ($q) use($id){
            $q->where('id', $id);
        })->select(['id', 'created_at', 'child_count'])->orderBy($order_by_key, $order_by_value)
            ->paginate(per_page());
        if ($list) {
            foreach ($list as $item) {
                $all = [
                    Balance::TYPE_VIP,
//                    Balance::TYPE_COOKBOOK_REC,
                    Balance::TYPE_MALL
                ];
                $total_amount = Balance::where('merchant_id', $item->id)->whereIn('type', $all)->sum('amount');
                $item->total_commission_yuan = format_yuan($total_amount);
            }
        }
        return $this->paginate($list);
    }
}

